package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.EmgCombatBo;
import com.ruoyi.system.domain.vo.EmgCombatVo;
import com.ruoyi.system.domain.EmgCombat;
import com.ruoyi.system.mapper.EmgCombatMapper;
import com.ruoyi.system.service.IEmgCombatService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 团队-作战记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-10
 */
@RequiredArgsConstructor
@Service
public class EmgCombatServiceImpl implements IEmgCombatService {

    private final EmgCombatMapper baseMapper;

    /**
     * 查询团队-作战记录
     */
    @Override
    public EmgCombatVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询团队-作战记录列表
     */
    @Override
    public TableDataInfo<EmgCombatVo> queryPageList(EmgCombatBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmgCombat> lqw = buildQueryWrapper(bo);
        Page<EmgCombatVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询团队-作战记录列表
     */
    @Override
    public List<EmgCombatVo> queryList(EmgCombatBo bo) {
        LambdaQueryWrapper<EmgCombat> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmgCombat> buildQueryWrapper(EmgCombatBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmgCombat> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCombatTypeId()), EmgCombat::getCombatTypeId, bo.getCombatTypeId());
        lqw.eq(StringUtils.isNotBlank(bo.getCombatDate()), EmgCombat::getCombatDate, bo.getCombatDate());
        lqw.eq(bo.getTeamId() != null, EmgCombat::getTeamId, bo.getTeamId());
        lqw.eq(bo.getUserId() != null, EmgCombat::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), EmgCombat::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getImage1()), EmgCombat::getImage1, bo.getImage1());
        lqw.eq(StringUtils.isNotBlank(bo.getImage2()), EmgCombat::getImage2, bo.getImage2());
        lqw.eq(StringUtils.isNotBlank(bo.getImage3()), EmgCombat::getImage3, bo.getImage3());
        lqw.eq(StringUtils.isNotBlank(bo.getImage4()), EmgCombat::getImage4, bo.getImage4());
        lqw.eq(StringUtils.isNotBlank(bo.getImage5()), EmgCombat::getImage5, bo.getImage5());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), EmgCombat::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodAt()), EmgCombat::getGoodAt, bo.getGoodAt());
        lqw.eq(StringUtils.isNotBlank(bo.getCombatExperience()), EmgCombat::getCombatExperience, bo.getCombatExperience());
        lqw.eq(StringUtils.isNotBlank(bo.getParam1()), EmgCombat::getParam1, bo.getParam1());
        lqw.eq(StringUtils.isNotBlank(bo.getParam2()), EmgCombat::getParam2, bo.getParam2());
        lqw.eq(StringUtils.isNotBlank(bo.getParam3()), EmgCombat::getParam3, bo.getParam3());
        lqw.eq(StringUtils.isNotBlank(bo.getParam4()), EmgCombat::getParam4, bo.getParam4());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EmgCombat::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增团队-作战记录
     */
    @Override
    public Boolean insertByBo(EmgCombatBo bo) {
        EmgCombat add = BeanUtil.toBean(bo, EmgCombat.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改团队-作战记录
     */
    @Override
    public Boolean updateByBo(EmgCombatBo bo) {
        EmgCombat update = BeanUtil.toBean(bo, EmgCombat.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EmgCombat entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除团队-作战记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
